********************************************************************************
**  TITLE:     us2016_uas_contact_networks                                    **  
**  AUTHOR:	   Philippe Mongrain                                              **
**  DATA:      Various datasets								                  **
**  DATE:	   October 2022 						                          **	
**  VERSION:   Stata 16					                                      **	
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close       			  			              
log using "us2016_uas_contact_networks", replace

* Open series 2

use "series2_July_orig.dta", clear

* Merge with series 6

merge 1:1 uasid using "series6_Aug_orig.dta", nogenerate force

* Merge with series 11

merge 1:1 uasid using "series11_Sep_orig.dta", nogenerate force

* Merge with post

merge 1:1 uasid using "post_election.dta", nogenerate force

* Series for own forecast

gen series_v = .

replace series_v = 2 if trump_win2!=. & clint_win2!=. & other_win2!=. & trump_win6 == . & clint_win6 == . & other_win6 == . & trump_win11 == . & clint_win11 == . & other_win11 == .
replace series_v = 6 if trump_win6!=. & clint_win6!=. & other_win6!=. & trump_win11 == . & clint_win11 == . & other_win11 == .
replace series_v = 11 if trump_win11!=. & clint_win11!=. & other_win11!=.


***************************
** INDEPENDENT VARIABLES **
***************************

* Time (series 2)

gen surveydate2=date(polldate2, "DMY", 2016)
format %tdMon_DD,_CCYY surveydate2

gen edate = 20161108

gen electiondate=date(string(edate,"%8.0f"),"YMD")

format %tdMon_DD,_CCYY electiondate

gen time2 = electiondate - surveydate2

* Time (series 6)

gen surveydate6=date(polldate6, "DMY", 2016)
format %tdMon_DD,_CCYY surveydate6

gen time6 = electiondate - surveydate6

* Time (series 11)

gen surveydate11=date(polldate11, "DMY", 2016)
format %tdMon_DD,_CCYY surveydate11

gen time11 = electiondate - surveydate11

* Time

gen time = .

replace time = time2 if series_v == 2
replace time = time6 if series_v == 6
replace time = time11 if series_v == 11

* Gender

gen male = .

replace male = gender2 if series_v == 2
replace male = 0 if gender6 == "female" & series_v == 6
replace male = 1 if gender6 == "male" & series_v == 6
replace male = 0 if gender11 == "female" & series_v == 11
replace male = 1 if gender11 == "male" & series_v == 11

* Age

replace age = . if age < 17

* Education

gen educ6 = substr(education6,1,2)
replace educ6 = subinstr(educ6, ".", "",.) 
destring educ6, replace

gen educ11 = substr(education11,1,2)
replace educ11 = subinstr(educ11, ".", "",.) 
destring educ11, replace

gen educ = .

replace educ = education2 if series_v == 2
replace educ = educ6 if series_v == 6
replace educ = educ11 if series_v == 11

* Vote status

gen vote2 = .

replace vote2 = 1 if prob_clint2 > prob_trump2 & prob_clint2 > prob_other2 & series_v == 2
replace vote2 = 1 if prob_clint2 > prob_trump2 & prob_clint2 == prob_other2 & series_v == 2
replace vote2 = 2 if prob_other2 > prob_trump2 & prob_other2 > prob_clint2 & series_v == 2
replace vote2 = 3 if prob_trump2 > prob_clint2 & prob_trump2 > prob_other2 & series_v == 2
replace vote2 = 3 if prob_trump2 > prob_clint2 & prob_trump2 == prob_other2 & series_v == 2

gen vote6 = .

replace vote6 = 1 if prob_clint6 > prob_trump6 & prob_clint6 > prob_other6 & series_v == 6
replace vote6 = 1 if prob_clint6 > prob_trump6 & prob_clint6 == prob_other6 & series_v == 6
replace vote6 = 2 if prob_other6 > prob_trump6 & prob_other6 > prob_clint6 & series_v == 6
replace vote6 = 3 if prob_trump6 > prob_clint6 & prob_trump6 > prob_other6 & series_v == 6
replace vote6 = 3 if prob_trump6 > prob_clint6 & prob_trump6 == prob_other6 & series_v == 6

gen vote11 = .

replace vote11 = 1 if prob_clint11 > prob_trump11 & prob_clint11 > prob_other11 & series_v == 11
replace vote11 = 1 if prob_clint11 > prob_trump11 & prob_clint11 == prob_other11 & series_v == 11
replace vote11 = 2 if prob_other11 > prob_trump11 & prob_other11 > prob_clint11 & series_v == 11
replace vote11 = 3 if prob_trump11 > prob_clint11 & prob_trump11 > prob_other11 & series_v == 11
replace vote11 = 3 if prob_trump11 > prob_clint11 & prob_trump11 == prob_other11 & series_v == 11

gen vote = .

replace vote = vote2 if series_v == 2
replace vote = vote6 if series_v == 6
replace vote = vote11 if series_v == 11

gen votestatus_whole = vote

label define votestatus 1 "Loser preference" 2 "Other" 3 "Winner preference" // Respondents who gave an equal probability to the three options were excluded
label values votestatus_whole votestatus

* Political disagreement

gen disagreement2 = .

replace  disagreement2 = 100 - clint_convt2 if vote == 1 & series_v == 2
replace disagreement2 = 100 - other_convt2 if vote == 2 & series_v == 2
replace disagreement2 = 100 - trump_convt2 if vote == 3 & series_v == 2

gen disagreement6 = .

replace  disagreement6 = 100 - clint_convt6 if vote == 1 & series_v == 6
replace disagreement6 = 100 - other_convt6 if vote == 2 & series_v == 6
replace disagreement6 = 100 - trump_convt6 if vote == 3 & series_v == 6

gen disagreement11 = .

replace  disagreement11 = 100 - clint_convt11 if vote == 1 & series_v == 11
replace disagreement11 = 100 - other_convt11 if vote == 2 & series_v == 11
replace disagreement11 = 100 - trump_convt11 if vote == 3 & series_v == 11

gen disagreement = .

replace disagreement = disagreement2 if series_v == 2
replace disagreement = disagreement6 if series_v == 6
replace disagreement = disagreement11 if series_v == 11 

gen disagreement_3pts = 0 if disagreement >= 0 & disagreement < 25
replace disagreement_3pts = 1 if disagreement > 24 & disagreement < 50
replace disagreement_3pts = 2 if disagreement > 49 & disagreement <= 100


*********************************
** NATIONAL-LEVEL EXPECTATIONS **
*********************************

* Forecasts

gen trumpchances2 = trump_win2
gen clintchances2 = clint_win2
gen otherchances2 = other_win2

gen trumpchances6 = trump_win6
gen clintchances6 = clint_win6
gen otherchances6 = other_win6

gen trumpchances11 = trump_win11
gen clintchances11 = clint_win11
gen otherchances11 = other_win11

gen complete2 = 1 if trumpchances2!=. & clintchances2!=. & otherchances2!=.
gen complete6 = 1 if trumpchances6!=. & clintchances6!=. & otherchances6!=.
gen complete11 = 1 if trumpchances11!=. & clintchances11!=. & otherchances11!=.

gen forecast_whole2 = 1 if trumpchances2 > clintchances2 & trumpchances2 > otherchances2 & complete2 == 1
replace forecast_whole2 = 2 if clintchances2 > trumpchances2 & clintchances2 > otherchances2 & complete2 == 1
replace forecast_whole2 = 3 if otherchances2 > trumpchances2 & otherchances2 > clintchances2 & complete2 == 1
replace forecast_whole2 = 99 if trumpchances2 == clintchances2 & complete2 == 1 | trumpchances2 == otherchances2 & complete2 == 1 | clintchances2 == otherchances2 & complete2 == 1

gen forecast_whole6 = 1 if trumpchances6 > clintchances6 & trumpchances6 > otherchances6 & complete6 == 1
replace forecast_whole6 = 2 if clintchances6 > trumpchances6 & clintchances6 > otherchances6 & complete6 == 1
replace forecast_whole6 = 3 if otherchances6 > trumpchances6 & otherchances6 > clintchances6 & complete6 == 1
replace forecast_whole6 = 99 if trumpchances6 == clintchances6 & complete6 == 1 | trumpchances6 == otherchances6 & complete6 == 1 | clintchances6 == otherchances6 & complete6 == 1

gen forecast_whole11 = 1 if trumpchances11 > clintchances11 & trumpchances11 > otherchances11 & complete11 == 1
replace forecast_whole11 = 2 if clintchances11 > trumpchances11 & clintchances11 > otherchances11 & complete11 == 1
replace forecast_whole11 = 3 if otherchances11 > trumpchances11 & otherchances11 > clintchances11 & complete11 == 1
replace forecast_whole11 = 99 if trumpchances11 == clintchances11 & complete11 == 1 | trumpchances11 == otherchances11 & complete11 == 1 | clintchances11 == otherchances11 & complete11 == 1

* Identify correct forecasts

gen correct_whole_d2 = 0 if forecast_whole2!=1 & forecast_whole2!=.
replace correct_whole_d2 = 1 if forecast_whole2 == 1

gen correct_whole_d6 = 0 if forecast_whole6!=1 & forecast_whole6!=.
replace correct_whole_d6 = 1 if forecast_whole6 == 1

gen correct_whole_d11 = 0 if forecast_whole11!=1 & forecast_whole11!=.
replace correct_whole_d11 = 1 if forecast_whole11 == 1

gen correct_whole_d = correct_whole_d2 if series_v == 2
replace correct_whole_d = correct_whole_d6 if series_v == 6
replace correct_whole_d = correct_whole_d11 if series_v == 11


**********
** SAVE **
**********

save "us2016_uas.dta", replace


******************************************
** MAIN ANALYSES: PRESIDENTIAL ELECTION **
******************************************

* Regression analysis and percentage of correct and incorrect forecasts (national)

logistic correct_whole_d disagreement votestatus_whole age i.male education time
estimates table, star(.05 .01 .001)

tab correct_whole_d if e(sample) == 1


***********************************************
** SUMMARY STATISTICS: PRESIDENTIAL ELECTION **
***********************************************

label variable correct_whole_d "Presidency winner (DV)"
label variable disagreement "Disagreement"
label variable votestatus_whole "National vote"
label variable age "Age"
label variable male "Gender (male = 1)"
label variable education "Education"
label variable time "Time of interview"

estpost tabstat correct_whole_d disagreement votestatus_whole age male education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "us2016_uas_summary_national.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.2f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2016 American presidential election (UAS), national level") replace

eststo clear

log close